<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
use app\index\model\Luntan;
use app\index\model\Comment;
use app\index\model\Report;
use app\index\model\Card;
use app\index\model\User1;
use app\index\model\Cardtype;
use think\facade\Session;
class Index2 extends controller
{
    public function index()
    {
        return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V5.1<br/><span style="font-size:30px">12载初心不改（2006-2018） - 你值得信赖的PHP框架</span></p></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=64890268" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="eab4b9f840753f8e7"></think>';
    }
    public function hello($name = 'ThinkPHP5')
    {
        return 'hello,' . $name;
    }
    //渲染添加论坛
    public function add(Request $request){
         $data=Cardtype::select();


        return $this->fetch('addcard',['data'=>$data]);
    }
    //添加论坛
    public function do_add(Request $request){
        $files=$_FILES['thumbs'];
        $count=count($files['tmp_name']);
            $thumb='';
            for($i=0;$i<$count;$i++){
                $filename='../public/images/'.time().$i.'.png';
               $str= move_uploaded_file($files['tmp_name'][$i], $filename);
                if($str){
               $thumb.=substr($filename,17).',';
                }
          }

        $addlun=new Luntan();
        $addlun->thumb= substr($thumb,1,-1);
        $addlun->title=$request->param('title');
        $addlun->content=$request->param('content');
        $addlun->times=time();
        $addlun->type_id=$request->param('type');
        if($addlun->save()){
           $this->success('发布成功');
        }else{
            $this->error('发布失败');
        }



//        $files = request()->file('thumbs');
////        var_dump($files);die;
//        foreach($files as $file){
//            // 移动到框架应用根目录/uploads/ 目录下
//            $info = $file->move( '../public/images');
//            if($info){
//                // 成功上传后 获取上传信息
//                // 输出 jpg
//                echo $info->getExtension();
//                // 输出 42a79759f284b767dfcb2a0197904287.jpg
//                echo $info->getFilename();
//            }else{
//                // 上传失败获取错误信息
//                echo $file->getError();
//            }
//        }
    }
 //论坛首页
    public function luntan(Request $request){
        $type=$request->param('type');
        if($type=='zui'){
            $data=Luntan::alias('l')
                ->join('user1 u','l.user_id=u.id')
                ->order('times', 'asc')
                ->limit(5)
                ->field('l.id,l.title,l.content,u.phone,l.sum,l.give,l.ping,l.times,l.user_id')
                ->select();

        }elseif($type=='re'){
            $data=Luntan::alias('l')
                ->join('user1 u','l.user_id=u.id')
                ->where('sum','>',20)
                ->field('l.id,l.title,l.content,u.phone,l.sum,l.give,l.ping,l.times,l.user_id')
                ->select();
        }elseif($type=='jing'){
            $data=Luntan::alias('l')
                ->join('user1 u','l.user_id=u.id')
    
                ->where('give','>',50)
                ->field('l.id,l.title,l.content,u.phone,l.sum,l.give,l.ping,l.times,l.user_id')
                ->select();
        }else{
            $data=Luntan::alias('l')
            ->join('user1 u','l.user_id=u.id')
                ->field('l.id,l.title,l.content,u.phone,l.sum,l.give,l.ping,l.times,l.user_id')
                ->select();
        }
        //论坛的类型
//        var_dump($data);
        $type=Cardtype::select();
        $arr=$this->count();
       // var_dump($type);
        return $this->fetch('luntan',['data'=>$data,'type'=>$type,'arr'=>$arr]);
    }
    //查询会员数 全部帖子数 今日帖子数
    public function count(){
        $count =Luntan::count('id');
        $morging = strtotime(date('Ymd'))+8*3600;
        $night=strtotime(date('Ymd'))+22*3600;
        $nowcount=Luntan::where('times','>',$morging)->where('times','<',$night)->count('id');
        $vip=Luntan::where('vip',1)->count('id');
        return ['count'=>$count,'nowcount'=>$nowcount,'vip'=>$vip];
    }
    //交流论坛
    public function jiaoliu(Request $request){
         $cardid=$request->param('cardid');
         $cardtype=Cardtype::find($cardid);
         $data=Luntan::alias('l')
                ->join('user1 u','l.user_id=u.id')
                ->field('l.id,l.title,l.content,u.phone,l.sum,l.give,l.ping,l.times')
                ->where('type_id',$cardid)->select();
        return $this->fetch('jiaoliu',['data'=>$data]);
    }
    //帖子的详情
    public function xiangqing(Request $request){
        $type=$request->param('type');
        $id=$request->param('id');
        $this->give($id);
        if($id){
            $data=Luntan::alias('l')
                ->join('user1 u','l.user_id=u.id')->field('l.id,l.title,l.content,u.phone,l.sum,l.give,l.ping,l.times')->where('l.id',$id)->find();
        }
        //上衣帖子
        if($type=='shang'){
            $id=$request->param('id');
            $id=$id-1<1?1:$id-1;
            $data=Luntan::alias('l')
                ->join('user1 u','l.user_id=u.id')
                ->field('l.id,l.title,l.content,u.phone,l.sum,l.give,l.ping,l.times')
                ->where('l.id',$id)->find();

        }
        //下一帖子
        if($type=='xia'){
            $id=$request->param('id');
            $id=$id+1>7?7:$id+1;
            $data=Luntan::alias('l')
                ->join('user1 u','l.user_id=u.id')
                ->field('l.id,l.title,l.content,u.phone,l.sum,l.give,l.ping,l.times')
                ->where('l.id',$id)->find();

        }

        Session::set('comment_id',$id);
        $user=new Luntan();
        //评论者的信息
        $arr=$user->user();
//var_dump($arr);
//       $content=Comment::whereIn('comment_id',$id)->select();
        return $this->fetch('ping',['data'=>$data,'arr'=>$arr]);
    }
    //浏览量
     public function give($id){
         $user=Luntan::where('id',$id)->find();
         $data=Luntan::get($id);
         $data->give=$user->give+1;
         $data->save();
     }
    //评论次数
//    public function ping(Request $request){
//        $id=$request->param('id');
//        $user=Luntan::where('id',$id)->find();
//        $data=Luntan::get($id);
//        $data->ping=$user->ping+1;
//        $data->save();
//
//    }
    //点赞
    public function upd(Request $request){
        $ids=$request->param('ids');
        if($request->isAjax()){
            $user=Luntan::where('id',$ids)->find();
            $data=Luntan::get($ids);
            $data->sum=$user->sum+1;
            $data->save();
            return  json_encode(['code'=>200,'msg'=>'点赞成功']);
        }else{
            $user=Luntan::where('id',$ids)->find();
            $data=Luntan::get($ids);
            $data->sum=$user->sum+1;
            if($data->save()){
                $this->success('点赞成功');
            }else{
                $this->success('点赞成功');
            }
        }
    }
    //举报
    public function report(Request $request){
        $id=$request->param('id');
        $user=Luntan::where('id',$id)->find();
        return $this->fetch('report',['user'=>$user]);

    }
    //举报完内容的添加
    public function do_report(Request $request){
        $id=$request->param('id');
        $content=$request->param('content');
        $data=new Report();
        $data->lun_id=$id;
        $data->content=$content;

        $data1=Report::where('lun_id',$id)->select()->toArray();
//        var_dump($data1);die;
        if(empty($data1)){
            if($data->save()){
                $this->success('举报成功');

            }else{
                $this->error('系统出现错误');
            }
        }else{
            $this->error('你的举报正在等待处理');
        }
    }
    public function task(Request $request){
        $id=$request->param('id');

        $data=Luntan::alias('l')
            ->join('user1 u','l.user_id=u.id')
            ->field('l.id,l.title,l.content,u.phone,l.times,l.user_id')
            ->where('l.id',$id)->find();
//        var_dump($data);die;
       $phone= substr_replace($data->phone, '****', 3, 4);
        return $this->fetch('task',['phone'=>$phone,'id'=>$data->user_id]);

    }
    public function cancel(Request $request){
        $id=$request->param('id');

        $data=User1::get($id);
        if($data->status==0){
            $data->status=1;
            $data->save();
            return json(['message'=>'关注成功']);
        }
        if($data->status==1){
            $data->status=0;
            $data->save();
            return json(['message'=>'取消关注成功']);
        }
    }
    public function location(Request $request){
        $id=$request->param('id');
        $user1=User1::where('id',$id)->find();
        if($user1->status==0){
            return json(['msg'=>'关注']);
        }else{
            return json(['msg'=>'取消']);
        }


    }
//    public function ce(){
//        $data=Luntan::get(1);
//        var_dump($data->status);
//    }
    //展示回复表单
     public function reply(Request $request){
           $userid=$request->param('userid');
         $commentid=$request->param('commentid');
         $comment=Comment::where('user_id',$userid)->where('comment_id',$commentid)->find();
//         var_dump($comment);die;
         return $this->fetch('reply',['comment'=>$comment]);

     }
    //修改到字段reply
    public function do_reply(Request $request){
        $userid=$request->param('userid');
        $commentid=$request->param('commentid');
        $reply=$request->param('reply');
        $comment=Comment::where('user_id',$userid)->where('comment_id',$commentid)
            ->update(['reply'=>$reply]);
        if($comment){
            $this->success('回复成功');
        }else{
            $this->error('回复失败');
        }
    }

}
